package com.dropdb.demo.service.impl;

import com.dropdb.demo.bean.SearchHistory;
import com.dropdb.demo.bean.SearchHistoryExample;
import com.dropdb.demo.bean.bo.BaseData;
import com.dropdb.demo.mapper.SearchHistoryMapper;
import com.dropdb.demo.service.SearchHistoryService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author:tanxin Desc:
 * @date:2020/6/27 1:38
 */
@Service
public class SearchHistoryServiceImpl implements SearchHistoryService {
    @Resource
    SearchHistoryMapper searchHistoryMapper;
    @Override
    public BaseData querySearchHistory(Integer page, Integer limit, String sort, String order, Integer userId, String keyword) {
        SearchHistoryExample searchHistoryExample = new SearchHistoryExample();
        SearchHistoryExample.Criteria criteria = searchHistoryExample.createCriteria();
        if (userId != null && keyword != null){
            criteria.andUserIdEqualTo(userId).andKeywordEqualTo(keyword);
        }else if (userId != null){
            criteria.andUserIdEqualTo(userId);
        }else if (keyword != null){
            criteria.andKeywordEqualTo(keyword);
        }
        searchHistoryExample.setOrderByClause(sort + " " + order);
        PageHelper.startPage(page,limit);
        List<SearchHistory> searchHistories = searchHistoryMapper.selectByExample(searchHistoryExample);
        PageInfo<SearchHistory> searchHistoryPageInfo = new PageInfo<>();
        long total = searchHistoryPageInfo.getTotal();
        return new BaseData(total,searchHistories);
    }
}
